var http = require('http');
http.createServer(function (request, response) {
    // 发送 HTTP 头部
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain。并用charset=UTF-8解决输出中文乱码
    response.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
 
    // 下句是发送响应数据
    response.end('Hello World! 这是简单的web服务器测试。\n');
}).listen(8888);
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

const express=require('express')
const bodyParser =require('body-parser')
const app=express()
const mysql = require('mysql')
app.use(bodyParser.json())
 
app.post('/',(req,res) => {
  console.log(req.body)
  res.json(req.body)
})
 
app.post('/show',(req,res)=>{
  console.log(req.body.name)
  const a=req.body.name
  var connection=mysql.createConnection({
    host:'localhost',
    user:'xxx',           //改成你的用户名
    password:'xxxxxx',   //改成你的密码
    database:'xxx'      //改成你的数据库名
  })
  connection.connect();
  connection.query("select * from tb_user",function(error,results,fields){  //改成你的表名和字段名
    if(error) throw console.error;
    res.json(results)
    console.log(results)
    
  })
  connection.end();
  
})
 
 
app.get('/',(req,res)=>{
  var connection = mysql.createConnection({
    host:'localhost',
    user:'root',           //改成你的用户名
    password:'root',   //改成你的密码
    database:'menbers_db'      //改成你的数据库名
  });
  connection.connect();
  connection.query('select * from tb_user',function(error,results,fields){    //改成你的表名和字段名
    if(error) throw error;
    res.json(results)
    // console.log(results)
  })
  connection.end();
})
 
app.listen(3000,()=>{
  console.log('server running at http://127.0.0.1:3000')
})